package org.shock.ss.sms.action.app;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.shock.ss.sms.action.BaseAction;
import org.shock.ss.sms.bean.app.CustomerInfo;
import org.shock.ss.sms.bean.app.InRecords;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.commons.utils.Util;
import org.shock.ss.sms.dao.app.impl.InRecordsDaoImpl;
import org.shock.ss.sms.service.app.InRecordsService;
import org.shock.ss.sms.service.app.impl.InRecordsServiceImpl;

import com.alibaba.fastjson.JSON;

public class InRecordsAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private InRecordsService cs = new InRecordsServiceImpl((InRecordsDaoImpl) DaoFactory.getDao("InRecordsDao"));
	private List inRecordsList;
	private InRecords inRecords = new InRecords();
	private String ids;
	private String customerinfo;

	public String inRecordsAdmin() throws Exception {
		return SUCCESS;
	}

	public String listInRecords() throws Exception {
		String sql = "";
		String sqlCount = "";
		if (!Util.isEmpty(inRecords.getProductcode())) {
			sqladd.append(" and productcode = '" + inRecords.getProductcode() + "'");
		}
		if (!Util.isEmpty(inRecords.getOrdercode())) {
			sqladd.append(" and ordercode = '" + inRecords.getOrdercode() + "'");
		}
		if (!Util.isEmpty(customerinfo)) {
			sqladd.append(" and customerid in(select id from " + CustomerInfo.getTable() + " where name like '%"
					+ customerinfo + "%' or phone like '%" + customerinfo + "%')");
		}
		if ("distinct".equals(action)) {
			sql = "select max(isck) as isck, productcode,ordercode,customerid,CONCAT(c.name,c.phone,c.province,c.city) as customername,sum(cargonum) as cargonum,max(intime) as intime from "
					+ InRecords.getTable()
					+ " a,"
					+ CustomerInfo.getTable()
					+ " c where a.customerid=c.id"
					+ sqladd
					+ " group by productcode,ordercode,customerid,c.name";
			sqlCount = "select count(distinct productcode) from " + InRecords.getTable() + " where 1=1" + sqladd;
		} else {
			sql = "select * from " + InRecords.getTable() + " a where 1=1" + sqladd;
			sqlCount = "select count(distinct productcode) from " + InRecords.getTable() + " where 1=1" + sqladd;
		}
		if (pageno == 1) {
			pageno = (start / rownum) + 1;
		}
		pager = cs.list(dir, "a." + sort, pageno, rownum, sqlCount, sql);
		totalCount = pager.getTotalCount();
		inRecordsList = pager.getList();
		jsonString = JSON.toJSONString(pager);
		System.out.println(jsonString);
		return SUCCESS;
	}

	public String viewInRecords() throws Exception {
		String sql = "select a.*,c.* from " + inRecords.getTable() + " a," + CustomerInfo.getTable()
				+ " c where a.customerid=c.id and productcode='" + inRecords.getProductcode() + "'";
		System.out.println(sql);
		Map map = cs.view(sql);
		jsonString = JSON.toJSONString(map);
		System.out.println(jsonString);
		return SUCCESS;
	}

	@RightAnno(rightValue = "delInRecords", rightNote = "删除入库单")
	public String delInRecords() throws Exception {
		ids = ids.replace(",", "','");
		String sql = "delete from " + inRecords.getTable() + " where isck=0 and productcode in (" + ids + ")";
		if ("cargodel".equals(action)) {
			sql = "delete from " + inRecords.getTable() + " where isck=0 and id in (" + ids + ")";
		}
		System.out.println(sql);
		cs.del(sql);
		success = true;
		message = "删除成功!";
		jsonString = "{success:" + success + ",message:'" + message + "'}";
		return SUCCESS;
	}

	@RightAnno(rightValue = "saveInRecords", rightNote = "保存入库单")
	public String saveInRecords() throws Exception {
		inRecords.setExcode("1-" + inRecords.getCargonum());
		if (null == inRecords.getId()) {
			return addInRecords();
		} else {
			return updateInRecords();
		}
	}

	public String updateInRecords() throws Exception {
		inRecords.setModifier(getUserInfor().getYgzh());
		inRecords.setLastmodifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
		cs.update(inRecords);
		return SUCCESS;
	}

	public String addInRecords() throws Exception {
		inRecords.setOp(getUserInfor().getYgzh());
		inRecords.setIntime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
		inRecords.setLastmodifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
		cs.add(inRecords);
		return SUCCESS;
	}

	public List getInRecordsList() {
		return inRecordsList;
	}

	public void setInRecordsList(List inRecordsList) {
		this.inRecordsList = inRecordsList;
	}

	public InRecords getInRecords() {
		return inRecords;
	}

	public void setInRecords(InRecords inRecords) {
		this.inRecords = inRecords;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getCustomerinfo() {
		return customerinfo;
	}

	public void setCustomerinfo(String customerinfo) {
		this.customerinfo = customerinfo;
	}

}
